package com.argrace.smart.thread;

import android.util.Log;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by argrace_dev_1 on 2018/1/29.
 */

public class MySendUdpThread extends Thread {
    private String add = null;

    private int port = 0;
    private String msg;

    public MySendUdpThread(String add, int port, String msg) {
        this.add = add;
        this.port = port;
        this.msg = msg;
    }

    @Override
    public void run() {
        super.run();
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            InetAddress group = InetAddress.getByName(add);
            String content = msg;
            DatagramPacket sendPacket
                    = new DatagramPacket(content.getBytes(), content.length(), group, port);
            for (int i = 0; i <= 5; i++) {
                socket.send(sendPacket);
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
